﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using GwLib.JqGridMvc;
using GwLib.SampleJqGrid.Models;

namespace GwLib.SampleJqGrid.Services
{
    public class PersonService
    {
        private static Random _rnd = new Random();
        private static List<Person> _allPersons = null;
        public PersonService()
        {
        }

        public List<Person> GetPersons(IGridDataParameters searchParameters)
        {
            var list = AllPersons;
            // filter and sort list through whatever method you wish (I prefer dynamic sql to do filtering and sorting)
            switch (searchParameters.SortField)
            {
                case "FirstName":
                    if (searchParameters.SortAscending) list = list.OrderBy(p => p.FirstName).ToList();
                    else list = list.OrderByDescending(p => p.FirstName).ToList();
                    break;
                case "LastName":
                    if (searchParameters.SortAscending) list = list.OrderBy(p => p.LastName).ToList();
                    else list = list.OrderByDescending(p => p.LastName).ToList();
                    break;
                case "BirthDate":
                    if (searchParameters.SortAscending) list = list.OrderBy(p => p.BirthDate).ToList();
                    else list = list.OrderByDescending(p => p.BirthDate).ToList();
                    break;
            }
            switch (searchParameters.SearchField)
            {
                case "FirstName":
                    if (searchParameters.SearchOperator == SearchOperator.Contains)
                        list = list.Where(p => p.FirstName.Contains(searchParameters.SearchString)).ToList();
                    else if (searchParameters.SearchOperator == SearchOperator.Equal)
                        list = list.Where(p => p.FirstName.Equals(searchParameters.SearchString, StringComparison.CurrentCultureIgnoreCase)).ToList();
                    break;
                case "LastName":
                    if (searchParameters.SearchOperator == SearchOperator.Contains)
                        list = list.Where(p => p.LastName.Contains(searchParameters.SearchString)).ToList();
                    else if (searchParameters.SearchOperator == SearchOperator.Equal)
                        list = list.Where(p => p.LastName.Equals(searchParameters.SearchString, StringComparison.CurrentCultureIgnoreCase)).ToList();
                    break;
            }

            // update parameters with the total record count
            searchParameters.TotalRecordCount = list.Count;

            int start = (searchParameters.PageNumber - 1) * searchParameters.RecordsPerPage,
                count = ((start+searchParameters.RecordsPerPage) > list.Count) ? (list.Count-start) : searchParameters.RecordsPerPage;

            list = list.GetRange(start, count);
            return list;
        }

        //--------------------------------------------------------------------------------------------
        // Silly list of Persons
        //--------------------------------------------------------------------------------------------
        private static List<Person> AllPersons
        {
            get
            {
                if (_allPersons == null)
                {
                    _allPersons = new List<Person>
                    {
                        new Person {
                            BirthDate = new DateTime(1975, 10, 28),
                            DeactivationDate = null,
                            Dependents = 7,
                            EmployeeNumber = 4823749,
                            FirstName = "Justin",
                            Gender = Gender.Male,
                            IsAdministrator = true,
                            LastName = "Greenwood",
                            Salary = 25432.23m
                        },
                        new Person {
                            BirthDate = new DateTime(1973, 11, 9),
                            DeactivationDate = null,
                            Dependents = 7,
                            EmployeeNumber = 4435234,
                            FirstName = "Aaron",
                            Gender = Gender.Male,
                            IsAdministrator = false,
                            LastName = "Greenwood",
                            Salary = 89384.4m
                        },
                        new Person {
                            BirthDate = new DateTime(1975, 10, 28),
                            DeactivationDate = null,
                            Dependents = 7,
                            EmployeeNumber = 4823749,
                            FirstName = "Kelly",
                            Gender = Gender.Female,
                            IsAdministrator = false,
                            LastName = "Greenwood",
                            Salary = 112342.32m
                        },
                        new Person {
                            BirthDate = new DateTime(2001, 2, 1),
                            DeactivationDate = new DateTime(2012, 3, 23),
                            Dependents = 0,
                            EmployeeNumber = 4823749,
                            FirstName = "Spencer",
                            Gender = Gender.Male,
                            IsAdministrator = false,
                            LastName = "Greenwood",
                            Salary = 8.5m
                        }
                    };

                    for (int i = 0; i < 1000; i++)
                    {
                        var p = new Person
                        {
                            BirthDate = new DateTime(_rnd.Next(1920, 2000), _rnd.Next(1, 13), _rnd.Next(1, 28)),
                            DeactivationDate = _rnd.Next(10) > 5 ? (DateTime?)null : new DateTime(_rnd.Next(2001, 2010), _rnd.Next(1, 13), _rnd.Next(1, 28)),
                            Dependents = _rnd.Next(10),
                            EmployeeNumber = _rnd.Next(333333,444444),
                            FirstName = RandomName(),
                            Gender = Gender.Male,
                            IsAdministrator = false,
                            LastName = RandomName(),
                            Salary = Convert.ToDecimal(_rnd.Next(2000000, 2000000))/100m
                        };
                        _allPersons.Add(p);
                    }
                }
                return _allPersons;
            }
        }

        public static string RandomName()
        {
            var vowels = new string[] { "a", "e", "i", "o", "u", "ou", "oui", "ea", "ai", "oa", "oo", "ie" };
            var cnst = new string[] { "q", "w", "r", "t", "y", "p", "s", "d", "f", "g", "h", "j", "k", "l", "l", "c", "v", "b", "n", "m", "th", "ch" };
            var sb = new StringBuilder();
            var len = _rnd.Next(4, 10);
            var s = _rnd.Next(0, 2);
            for (int i = 0; i < len; i++) sb.Append(((s++ % 2) == 0) ? vowels[_rnd.Next(0, vowels.Length)] : cnst[_rnd.Next(0, cnst.Length)]);
            sb[0] = Char.ToUpper(sb[0]);
            return sb.ToString();
        }
    }
}